package com.migo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.migo.dao.TutorialVideoDao;
import com.migo.entity.TeachingVideoEntity;
import com.migo.entity.TutorialVideoDetail;
import com.migo.entity.TutorialVideoEntity;
import com.migo.service.TutorialVideoService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xianhao.fan
 * @date 2022年01月05日 14:14
 */
@Service
public class TutorialVideoServiceImpl implements TutorialVideoService {

    @Autowired
    TutorialVideoDao tutorialVideoDao;


    @Override
    public List<Map<String,Object>> queryHomePageData(String proId, String language) {
        //查询分栏开放情况
        List<Map<String,Object>> videoData = tutorialVideoDao.queryVideoData(proId, language);
        if(videoData==null||videoData.size()==0){
            return null;
        }
        //查询分栏视频列表
        Map<String, Object> paramMap = new HashedMap();
        paramMap.put("proId", proId);
        paramMap.put("language", language);
        List<TutorialVideoEntity> lists = tutorialVideoDao.queryList(paramMap);

        if(lists==null||lists.size()==0){
            return null;
        }

        for(int i = 0;i<videoData.size();i++){
            Map map = videoData.get(i);
            List<TutorialVideoEntity> entitys = new ArrayList<>();
            for(TutorialVideoEntity tutorialVideoEntity:lists){
                if(map.get("typeId").equals(tutorialVideoEntity.getColumnsid())){
                    entitys.add(tutorialVideoEntity);
                }
            }
            map.put("videoEntitys",entitys);
            videoData.set(i,map);
        }
        return videoData;
    }

    @Override
    public List<TutorialVideoEntity> queryList(Map<String, Object> map) {
        return tutorialVideoDao.queryList(map);
    }

    @Override
    public TutorialVideoEntity queryObject(Integer tutorialVideoid) {
        TutorialVideoEntity tutorialVideoEntity = tutorialVideoDao.queryObject(tutorialVideoid);
        //查询视频详细信息
        ArrayList<Map<String,Object>> list = tutorialVideoDao.tutorialVideoDetail(tutorialVideoid);
        tutorialVideoEntity.setVideoDetails(list);
        return tutorialVideoEntity;
    }

}
